﻿using System;
using System.Drawing;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace TouchUI
{
    public class ScrollList : ScrollControl
    {
        public List<ScrollListItem> ListItems { get; set; }
        public int ListItemHeight { get; set; }
        public Brush HighBrush { get; set; }
        public Brush GrayBrush { get; set; }
        public int SelectedLine { get; set; }

        public ScrollList()
        {
            ListItems = new List<ScrollListItem>();
            SelectedLine = -1;
        }

        #region Events
        public event EventHandler Selected;
        protected virtual void OnSelected(EventArgs e) 
        {
            if (Selected != null)
                Selected(this, e);
        }
        #endregion

        public override void Paint(Graphics g)
        {
            int firstListItem = Math.Max(-(ScrollTop / ListItemHeight), 0);
            int lastListItem = Math.Min(firstListItem + (Rectangle.Height / ListItemHeight) + 2, ListItems.Count);
            for(int i = firstListItem; i < lastListItem; i++)
            {
                if(i == SelectedLine)
                {
                    ListItems[i].Selected = true;
                    g.FillRectangle(HighBrush, new Rectangle(0, Rectangle.Top + ListItemHeight * i + ScrollTop, Rectangle.Width, ListItemHeight));
                }
                else
                {
                    ListItems[i].Selected = false;
                    if(i % 2 == 1)
                        g.FillRectangle(GrayBrush, new Rectangle(0, Rectangle.Top + ListItemHeight * i + ScrollTop, Rectangle.Width, ListItemHeight));
                }

                ListItems[i].Rectangle = new Rectangle(0, Rectangle.Top + ListItemHeight * i + ScrollTop, Rectangle.Width, ListItemHeight);
                ListItems[i].Paint(g);
            }
        }

        public override void MouseUp(Point p)
        {
            base.MouseUp(p);

            if(PointSelected)
            {
                int y = p.Y - ScrollTop;
                if(y > 0 && y < ListItemHeight * ListItems.Count)
                {
                    int line = y / (ListItemHeight);
                    SelectedLine = line;
                    OnSelected(EventArgs.Empty);
                }
            }
        }

        public override void KeyUp(KeyEventArgs e)
        {
            base.KeyUp(e);

            bool changedLine = false;
            if(e.KeyCode == Keys.Up)
            {
                if(SelectedLine > 0)
                {
                    SelectedLine--;
                    changedLine = true;
                }
            }
            if(e.KeyCode == Keys.Down)
            {
                if(SelectedLine < ListItems.Count - 1)
                {
                    SelectedLine++;
                    changedLine = true;
                }
            }
            if(changedLine)
            {
                int topPosition = ListItemHeight * SelectedLine + ScrollTop;
                if(topPosition < 0)
                    ScrollTop -= topPosition;

                int bottomPosition = ListItemHeight * SelectedLine + ListItemHeight + ScrollTop;
                if(bottomPosition > Rectangle.Height - Rectangle.Top)
                    ScrollTop += Rectangle.Height - Rectangle.Top - bottomPosition;
            }
            if(e.KeyCode == Keys.Enter)
            {
                if(SelectedLine > -1)
                    OnSelected(EventArgs.Empty);
            }
            e.Handled = true;
        }

        public override void DoAutomaticMotion()
        {
            base.DoAutomaticMotion();
        }
    }
}
